package com.will.txj.aj.security.config.auth;


import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author: wen-yi;
 * @date: 2021/12/5 21:44;
 * @Description: 用于成功失败处理输出JSON
 */
public class JSONAuthenticationUtil {
    @SneakyThrows
    public static void WriteJSON(HttpServletRequest request, HttpServletResponse response, Object data){
        //这里很重要，否则页面获取不到正常的JSON数据集
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Method", "POST,GET");
        //输出JSON
        PrintWriter out = response.getWriter();
        out.write(JSONObject.toJSONString(data));
        out.flush();
        out.close();
    }
}
